package com.windoent.yugioh.ui.fragment;

import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.baidu.mobstat.StatService;
import com.windoent.yugioh.R;
import com.windoent.yugioh.base.BaseFragment;
import com.windoent.yugioh.bean.RankListBean;
import com.windoent.yugioh.bean.eventbus.BottomViewEvent;
import com.windoent.yugioh.bean.eventbus.MessageEvent;
import com.windoent.yugioh.bean.eventbus.PointEvent;
import com.windoent.yugioh.ui.activity.point.InstructionSeasonIntegralActivity;
import com.windoent.yugioh.ui.fragment.point.JanePointFragment;
import com.windoent.yugioh.ui.fragment.point.JapanPointFragment;
import com.windoent.yugioh.utils.ToastUtils;
import com.windoent.yugioh.widget.popupwindow.PointPopUpWindow;
import com.youth.banner.util.BannerUtils;
import com.zhy.http.okhttp.OkHttpUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

/**
 * author : robert
 * date   : 2021/4/28 18:11
 * desc   :积分
 */
public class PointFragment extends BaseFragment {
    private LinearLayout mLlShaixuan;
    private LinearLayout mLlInstructions;
    private TextView mTvJane;
    private TextView mTvJapan;
    private ViewPager mVp;
    private JanePointFragment janePointFragment = JanePointFragment.newsInstance();
    private JapanPointFragment japanPointFragment = JapanPointFragment.newsInstance();
    private List<Fragment> listFragments = new ArrayList<>();
    private MyAdapter mAdapter;
    private List<RankListBean.DataBean.AllSeasonBean> data;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_point;
    }

    public static PointFragment newsInstance() {
        return new PointFragment();
    }

    @Override
    protected void initEventAndData() {
        initView();
        initData();
        initListener();
    }

    private void initListener() {
        mTvJane.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setTab(0);
            }
        });
        mTvJapan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setTab(1);
            }
        });
        mLlShaixuan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //筛选
                if (data != null) {
                    showFiltraDialog(data);
                } else ToastUtils.showText(getString(R.string.string_nodata_filter_point_warn));
            }
        });
        mLlInstructions.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //跳转到【赛季积分说明】页面
                startActivity(new Intent(mContext, InstructionSeasonIntegralActivity.class));
            }
        });

    }

    private void showFiltraDialog(List<RankListBean.DataBean.AllSeasonBean> data) {
        new PointPopUpWindow(mContext, data, BannerUtils.dp2px(16), new PointPopUpWindow.OnInterface() {
            @Override
            public void onSelect(RankListBean.DataBean.AllSeasonBean s) {
                //发送eventbus粘性事件
                MessageEvent event = new MessageEvent();
                event.setMessage(s.getId() + "");
                event.setType(mVp.getCurrentItem());
                EventBus.getDefault().postSticky(event);
            }
        }).showAtBottom(mLlShaixuan, 0, 0);
    }

    private void initData() {
        EventBus.getDefault().register(this);
        listFragments.clear();
        listFragments.add(janePointFragment);
        listFragments.add(japanPointFragment);
        mAdapter = new MyAdapter(getChildFragmentManager());
        mVp.setAdapter(mAdapter);
        mVp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                int CurrentItem = mVp.getCurrentItem();
                setSelectFragment(CurrentItem);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
        setTab(0);
    }

    private void initView() {
        mLlShaixuan = (LinearLayout) mView.findViewById(R.id.ll_shaixuan);
        mLlInstructions = (LinearLayout) mView.findViewById(R.id.ll_instructions);
        mTvJane = (TextView) mView.findViewById(R.id.tv_jane);
        mTvJapan = (TextView) mView.findViewById(R.id.tv_japan);
        mVp = (ViewPager) mView.findViewById(R.id.vp);
    }

    private void setTab(int i) {
        setSelectFragment(i);
        mVp.setCurrentItem(i);
    }

    private void setSelectFragment(int i) {
        resetSelect();
        switch (i) {
            case 0:
                mTvJane.setSelected(true);
                break;
            case 1:
                mTvJapan.setSelected(true);
                break;
            default:
                break;
        }

    }

    private void resetSelect() {
        mTvJane.setSelected(false);
        mTvJapan.setSelected(false);
    }

    class MyAdapter extends FragmentPagerAdapter {

        public MyAdapter(FragmentManager fm) {
            super(fm, FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);

        }

        @Override
        public Fragment getItem(int position) {
            return listFragments.get(position);
        }

        @Override
        public int getCount() {
            return listFragments.size();

        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void Event(PointEvent event) {
        int type = event.getType();
        if (type == 0) {
            data = event.getData();
        } else if (type == 1) {
            data = event.getData();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        //解除注册EventBus
        EventBus.getDefault().unregister(this);
        OkHttpUtils.getInstance().cancelTag(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        if (getActivity() != null) {
            // 页面埋点
            StatService.onPageEnd(getActivity(), "积分首页列表");
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (getActivity() != null) {
            // 页面埋点
            StatService.onPageStart(getActivity(), "积分首页列表");
        }
    }
}
